package com.fbd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fbd.common.BaseContext;
import com.fbd.common.CustomException;
import com.fbd.entity.*;
import com.fbd.mapper.OrdersMapper;
import com.fbd.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private UserService userService;
    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Override
    public void submit(Orders orders) {
        //1、获取用户id
        Long currentId = BaseContext.getCurrentId();
        //2、查询当前用户购物车数据
        LambdaQueryWrapper<ShoppingCart> wrapper= new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,currentId);
        //得到购物车数据集合
        List<ShoppingCart> shoppingCarts = shoppingCartService.list(wrapper);

        //判断购物车是否为空
        if(shoppingCarts == null || shoppingCarts.size() == 0){
            throw new CustomException("购物车数据为空，不能下单");
        }
        //用int类型在多线程中可能算错
        AtomicInteger atomicInteger = new AtomicInteger();
        //生成订单号
        long orderId = IdWorker.getId();
        //遍历购物车数据
        for(ShoppingCart shoppingCart :shoppingCarts){
            //订单金额=每个菜品数量x菜品价格之和
            int i = shoppingCart.getAmount().multiply(new BigDecimal(shoppingCart.getNumber())).intValue();
            atomicInteger.addAndGet(i);
            //将购物车数据存放在订单明细中
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setName(shoppingCart.getName());
            orderDetail.setImage(shoppingCart.getImage());
            orderDetail.setOrderId(orderId);
            orderDetail.setDishId(shoppingCart.getDishId());
            orderDetail.setDishFlavor(shoppingCart.getDishFlavor());
            orderDetail.setNumber(shoppingCart.getNumber());
            orderDetail.setAmount(shoppingCart.getAmount());
            //将的到的数据存入数据库中
            orderDetailService.save(orderDetail);
        }
        //查询用户数据
        User user = userService.getById(currentId);

        //查询地址信息
        Long addressBookId = orders.getAddressBookId();
        AddressBook addressBook = addressBookService.getById(addressBookId);
        if (addressBook == null){
            throw new CustomException("用户地址信息有误，不能下单");
        }

        //设置订单的属性
        orders.setId(orderId);
        orders.setNumber(String.valueOf(orderId));
        orders.setStatus(2);
        orders.setUserId(currentId);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setPayMethod(1);
        orders.setAmount(new BigDecimal(atomicInteger.get()));
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setUserName(user.getName());
        orders.setConsignee(addressBook.getConsignee());
        //将订单数据添加至数据库中
        this.save(orders);
        //根据用户id删除购物车数据
        LambdaQueryWrapper<ShoppingCart> shoppingCartWrapper = new LambdaQueryWrapper<>();
        shoppingCartWrapper.eq(ShoppingCart::getUserId,currentId);
        shoppingCartService.remove(shoppingCartWrapper);
    }
}
